/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Dec 13, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.gmatrix.control;

import edu.psu.geovista.app.gmatrix.JDfMatrix;
import edu.psu.geovista.app.gmatrix.canvas.DfMatrixCanvas;
import edu.psu.geovista.app.gmatrix.component.MatrixHeader;
import edu.psu.geovista.app.gmatrix.data.DfSelectionModel;
import edu.psu.geovista.app.gmatrix.event.MatrixSelectionEvent;
import edu.psu.geovista.app.gmatrix.event.MatrixSelectionListener;
import edu.psu.geovista.app.gmatrix.toolkit.MatrixLoggerMgr;
import edu.psu.geovista.app.gmatrix.toolkit.MatrixUtils;

import javax.swing.*;
import java.awt.*;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DfSelectMgr implements MatrixSelectionListener, SelectClient {
    private static final Logger logger=Logger.getLogger(DfSelectMgr.class.getName());
    JDfMatrix matrix;
    //DfSelectionModel selectmodel;

    public DfSelectMgr(JDfMatrix matrix) {
        this.matrix = matrix;
    }

    public void select(Set<Point> select){
         matrix.select(select);
    }
    public void selectRow(int[] rowIndexs){
         matrix.selectRow(rowIndexs);
    }
    public void addRow(int[] rowIndexs){
        Set<Point> select = matrix.getCellsByRows(rowIndexs);
        DfSelectionModel selectmodel = this.matrix.getSelectmodel();
        selectmodel.addSelection(select);
    }
    public void removeRow(int[] rowIndexs){
        Set<Point> select = matrix.getCellsByRows(rowIndexs);
        DfSelectionModel selectmodel = this.matrix.getSelectmodel();
        selectmodel.removeSelection(select);
    }
     public void combineRow(int rowIndexs){
         //todo
    }

     public void selectColumn(int[] colndexs){
        matrix.selectColumn(colndexs);
    }
    public void addColumn(int[] colIndexs){
         Set<Point> select = matrix.getCellsByColumns(colIndexs) ;
        DfSelectionModel selectmodel = this.matrix.getSelectmodel();
        selectmodel.addSelection(select);
    }
    public void removeColumn(int[] colIndexs){
        Set<Point> select = matrix.getCellsByColumns(colIndexs) ;
        DfSelectionModel selectmodel = this.matrix.getSelectmodel();
        selectmodel.removeSelection(select);
    }

    public void matrixSelectionChanged(MatrixSelectionEvent e) {
        logger.fine(" ");
        final Set<Point> selection = e.getSelection();
        int[] rows = matrix.getRowsOfCells(selection);
        int[] cols = matrix.getColumnsOfCells(selection);

        DfMatrixCanvas canvas = matrix.getCanvas();
        canvas.select(selection,false);//not fire events
        MatrixLoggerMgr.fine(logger,"     Select cells "+selection.size() );
        if(logger.isLoggable(Level.FINE ) )    {
            MatrixUtils.print(selection,"        ");
        }

        MatrixHeader rowheader = matrix.getRowheader();
        rowheader.setSelection(rows);
        MatrixLoggerMgr.fine(logger,"     Select rowheader rows");
        if(logger.isLoggable(Level.FINE ) )    {
            MatrixUtils.print(rows,"        ");
        }

        MatrixHeader columnheader = matrix.getColumnheader();
        columnheader.setSelection(cols);
        MatrixLoggerMgr.fine(logger,"     Select column header columns");
        if(logger.isLoggable(Level.FINE ) )    {
            MatrixUtils.print(cols,"        ");
        }

        matrix.repaint();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                matrix.fireSelectionChanged(selection);
            }
        } );


    }





}
